import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/store/modules/user'
import Layout from '@/layout/index.vue'

// 路由配置
const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login/index.vue'),
    meta: { title: '登录', hidden: true }
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index.vue'),
        meta: { title: '仪表盘', icon: 'dashboard', affix: true }
      }
    ]
  },
  {
    path: '/categories',
    component: Layout,
    children: [
      {
        path: 'index',
        name: 'Categories',
        component: () => import('@/views/categories/index.vue'),
        meta: { title: '商品分类', icon: 'category' }
      }
    ]
  },
  {
    path: '/products',
    component: Layout,
    redirect: '/products/index',
    children: [
      {
        path: 'index',
        name: 'Products',
        component: () => import('@/views/products/product-list.vue'),
        meta: { title: '商品管理', icon: 'product' }
      },
      {
        path: 'edit',
        name: 'ProductCreate',
        component: () => import('@/views/products/product-edit.vue'),
        meta: { title: '新增商品', hidden: true }
      },
      {
        path: 'edit/:id',
        name: 'ProductEdit',
        component: () => import('@/views/products/product-edit.vue'),
        meta: { title: '编辑商品', hidden: true }
      }
    ]
  },
  {
    path: '/orders',
    component: Layout,
    children: [
      {
        path: 'index',
        name: 'Orders',
        component: () => import('@/views/orders/index.vue'),
        meta: { title: '订单管理', icon: 'order' }
      },
      {
        path: ':id',
        name: 'OrderDetail',
        component: () => import('@/views/orders/OrderDetail.vue'),
        meta: { title: '订单详情', hidden: true }
      }
    ]
  },
  {
    path: '/banners',
    component: Layout,
    children: [
      {
        path: 'index',
        name: 'Banners',
        component: () => import('@/views/banners/index.vue'),
        meta: { title: '轮播图管理', icon: 'banner' }
      }
    ]
  },
  {
    path: '/settings',
    component: Layout,
    meta: { title: '系统管理', icon: 'system' },
    children: [
      {
        path: 'config',
        name: 'Config',
        component: () => import('@/views/settings/config/index.vue'),
        meta: { title: '系统设置' }
      },
      {
        path: 'admin',
        name: 'Admin',
        component: () => import('@/views/settings/admin/index.vue'),
        meta: { title: '管理员管理' }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach(async (to, from, next) => {
  const userStore = useUserStore()
  const token = userStore.token

  // 设置页面标题
  document.title = to.meta.title ? `${to.meta.title} - 商城管理系统` : '商城管理系统'

  if (token) {
    if (to.path === '/login') {
      next('/')
    } else {
      // 获取用户信息
      if (!userStore.userInfo) {
        try {
          await userStore.getUserInfo()
        } catch (error) {
          // token失效，重新登录
          await userStore.logout()
          next(`/login?redirect=${to.path}`)
          return
        }
      }
      next()
    }
  } else {
    if (to.path === '/login') {
      next()
    } else {
      next(`/login?redirect=${to.path}`)
    }
  }
})

export default router 